package main;

import handler.ServerHandler;

import java.net.Socket;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: qiukexu
 * Date: 2009-6-2
 * Time: 14:52:00
 */
public class ServerThread extends Thread {
    Socket socket = null;
    int clientCount = 0;
    String remoteAddr = null;

    public ServerThread(Socket socket, int clientCount) {
        this.clientCount = clientCount + 1;
        this.socket = socket;
        this.remoteAddr = socket.getRemoteSocketAddress().toString();
        System.out.println("No." + this.clientCount + " Client Has Been Connected From "+remoteAddr+".");
    }

    @Override
    public void run() {
        try {
            new ServerHandler().doHandle(socket, this.clientCount);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            socket = null;
            System.out.println("No." + this.clientCount + " Client Has Been Disconnected From "+remoteAddr+".");
        }
    }
}
